package com.mhl.adapter;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.fragment.app.FragmentActivity;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.mhl.campuscabinet.ChatroomActivity;
import com.mhl.campuscabinet.MainActivity;
import com.mhl.campuscabinet.R;
import com.mhl.client.HttpUtil;
import com.mhl.client.OkHttpUtils;
import com.mhl.entity.Message;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Response;

public class MessageAdapter extends RecyclerView.Adapter<MessageAdapter.ViewHolder> {
    // 需要展示的消息实体列表
    List<Message> messageList = new ArrayList<>();
    // 上下文
    MainActivity activity;

    static class ViewHolder extends RecyclerView.ViewHolder {
        View messageView;
        ImageView profile;
        TextView username;
        TextView latestMsg;

        public ViewHolder(@NonNull @NotNull View view) {
            super(view);
            messageView = view;
            profile = view.findViewById(R.id.profile);
            username = view.findViewById(R.id.username);
            latestMsg = view.findViewById(R.id.latest_message);
        }
    }
    // 构造方法
    public MessageAdapter(List<Message> messageList, MainActivity activity) {
        this.activity = activity;
        this.messageList = messageList;
    }
    @NonNull
    @NotNull
    @Override
    public MessageAdapter.ViewHolder onCreateViewHolder(@NonNull @NotNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.message_item, parent, false);
        final ViewHolder holder = new ViewHolder(view);
        holder.messageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int position = holder.getAdapterPosition();
                Message message = messageList.get(position);
                Intent intent = new Intent(activity, ChatroomActivity.class);
                activity.startActivity(intent);
            }
        });
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull @NotNull MessageAdapter.ViewHolder holder, int position) {
        Message message = messageList.get(position);
        Glide.with(activity).load("https://img2.baidu.com/it/u=3329353302,71880253&fm=26&fmt=auto").into(holder.profile);
        holder.username.setText(message.getUsername());
        System.out.println("对方的用户名为" + message.getUsername());
        holder.latestMsg.setText(message.getLatestMsg());
    }

    @Override
    public int getItemCount() {
        return messageList.size();
    }
}
